package com.wlf.demo.pageHelper.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wlf.demo.pageHelper.dao.UserDao;
import com.wlf.demo.pageHelper.model.Pagination;
import com.wlf.demo.pageHelper.model.converter.UserMapper;
import com.wlf.demo.pageHelper.model.dto.UserDTO;
import com.wlf.demo.pageHelper.model.po.User;
import com.wlf.demo.pageHelper.model.vo.UserVO;
import com.wlf.demo.pageHelper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDao userDao;

    @Override
    public Pagination<UserVO> findByPage(UserDTO userDTO, int pageNum, int pageSize, String order) {
        final Pagination<UserVO> page = new Pagination<UserVO>();

        User user = userMapper.dtoToPO(userDTO);
        PageHelper.startPage(pageNum, pageSize, order);
        Page<User> users = userDao.selectByPage(user, pageNum, pageSize);

        page.build(users)
            .build(userMapper.poToVO(users.getResult()));

        return page;
    }
}
